<?php

class Almanac {
	private $message ;
	private $d = null;
	private $year ;
	private $month ;
	private $day ;
	private $cYear;
	private $cMonth;
	private $cDay;
	private $seed ;
	private $CalendarData =  array(0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95);
	private $tgString = "甲乙丙丁戊己庚辛壬癸";
	private $dzString = "子丑寅卯辰巳午未申酉戌亥";
	private $numString = "一二三四五六七八九十";
	private $monString = "正二三四五六七八九十冬腊";
	private $weekString = "日一二三四五六";
	private $sx = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
	private $avatars = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50);
    private $list ;
    private $madd = array() ;
    private $listString = '[{"name":"看AV", "good":"释放压力，重铸自我","bad":"会被家人撞到"},{"name":"组模型", "good":"今天的喷漆会很完美","bad":"精神不集中板件被剪断了"},{"name":"投稿情感区", "good":"问题圆满解决","bad":"会被当事人发现"},{"name":"逛匿名版", "good":"今天也要兵库北","bad":"看到丧尸在晒妹"},{"name":"和女神聊天", "good":"女神好感度上升","bad":"我去洗澡了，呵呵"},{"name":"啪啪啪", "good":"啪啪啪啪啪啪啪","bad":"会卡在里面"},{"name":"熬夜", "good":"夜间的效率更高","bad":"明天有很重要的事"},{"name":"锻炼", "good":"八分钟给你比利般的身材","bad":"会拉伤肌肉"},{"name":"散步", "good":"遇到妹子主动搭讪","bad":"走路会踩到水坑"},{"name":"打排位赛", "good":"遇到大腿上分500","bad":"我方三人挂机"},{"name":"汇报工作", "good":"被夸奖工作认真","bad":"上班偷玩游戏被扣工资"},{"name":"抚摸猫咪", "good":"才不是特意蹭你的呢","bad":"死开！愚蠢的人类"},{"name":"遛狗", "good":"遇见女神遛狗搭讪","bad":"狗狗随地大小便被罚款"},{"name":"烹饪", "good":"黑暗料理界就由我来打败","bad":"难道这就是……仰望星空派？"},{"name":"告白", "good":"其实我也喜欢你好久了","bad":"对不起，你是一个好人"},{"name":"求站内信", "good":"最新种子入手","bad":"收到有码葫芦娃"},{"name":"追新番", "good":"完结之前我绝不会死","bad":"会被剧透"},{"name":"打卡日常", "good":"怒回首页","bad":"会被老板发现"},{"name":"下副本", "good":"配合默契一次通过","bad":"会被灭到散团"},{"name":"抢沙发", "good":"沙发入手弹无虚发","bad":"会被挂起来羞耻play"},{"name":"网购", "good":"商品大减价","bad":"问题产品需要退换"},{"name":"跳槽", "good":"新工作待遇大幅提升","bad":"再忍一忍就加薪了"},{"name":"读书", "good":"知识就是力量","bad":"注意力完全无法集中"},{"name":"早睡", "good":"早睡早起方能养生","bad":"会在半夜醒来，然后失眠"},{"name":"逛街", "good":"物美价廉大优惠","bad":"会遇到奸商"}]';
    function Almanac() {
    	$this->year = (int)date("Y", time() + 8 * 3600);
    	$this->month = (int)date("m", time() + 8 * 3600);
    	$this->day = (int)date("d", time() + 8 * 3600);
    	$this->getSeed();
    	$this->list = json_decode($this->listString) ;
    	$this->madd[0] = 0;
		$this->madd[1] = 31;
		$this->madd[2] = 59;
		$this->madd[3] = 90;
		$this->madd[4] = 120;
		$this->madd[5] = 151;
		$this->madd[6] = 181;
		$this->madd[7] = 212;
		$this->madd[8] = 243;
		$this->madd[9] = 273;
		$this->madd[10] = 304;
		$this->madd[11] = 334;
		
    }
    
    public function run() {
      $date = date("Y-m-d", time() + 8 * 3600);
      $img = ROOT_PATH . "Temp/" .md5("yaya".$date) . ".png";
      $url = "http://127.0.0.1/SoniaRobot/Temp/" .md5("yaya".$date) . ".png";
      //echo $img;
      if (!file_exists($img)) {
     // echo "CutyCapt --url=http://localhost/SoniaRobot/Manager/html/ac.html --out=".$img;
        exec("CutyCapt --url=http://localhost/SoniaRobot/Manager/html/ac.html --out=".$img);
        $dst_h = 380;
        $dst_w = 800;
        list ($src_w, $src_h) = getimagesize($url);
        $dst_scale = $dst_h / $dst_w; //目标图像长宽比
        $src_scale = $src_h / $src_w; // 原图长宽比

        if ($src_scale >= $dst_scale) {
          // 过高
          $w = intval($src_w);
          $h = intval($dst_scale * $w);
          $x = 0;
          $y = ($src_h - $h) / 3;
        } else {
          // 过宽
          $h = intval($src_h);
          $w = intval($h / $dst_scale);
          $x = ($src_w - $w) / 2;
          $y = 0;
        }
        // 剪裁
        $source = imagecreatefrompng($url);
        $croped = imagecreatetruecolor($w, $h);
        imagecopy($croped, $source, 0, 0, $x, $y, $src_w, $src_h);
        // 缩放
        $scale = $dst_w / $w;
        $target = imagecreatetruecolor($dst_w, $dst_h);
        $final_w = intval($w * $scale);
        $final_h = intval($h * $scale);
        imagecopyresized($target, $croped, 0, 0, 0, 0, $final_w, $final_h, $w, $h);
        // 保存
        $timestamp = "Temp/".md5("yaya".$date).".jpg";
        imagejpeg($target, "{$timestamp}");
        imagedestroy($target);
        
      }
      $url = "http://127.0.0.1/SoniaRobot/Temp/" .md5("yaya".$date) . ".jpg";
    /**
		//print_r($this->list);
		//echo $this->seed;
	 	$sg = $this->rnd($this->seed, 8) % 100;
	 	// $sg;
		$sb = $this->rnd($this->seed, 4) % 100;
		$mess = "宜：\n";
		for ($i = 0, $l = $this->rnd($this->seed, 9) % 3 + 2; $i < $l ; $i++) {
			$n = (int)($sg * 0.01 * count($this->list));
			$a = $this->list[$n];
			$m = (int)($this->rnd($this->seed, (3 + $i)) % 100 * 0.01 * count($this->avatars));
			$mess .= getface($this->avatars[$m])."【". $a->name."】 ". $a->good."\n";
			array_splice($this->list,$n, 1);
			array_splice($this->avatars,$m, 1);
		}
		$mess .="忌：\n";
		for ($i = 0, $l = $this->rnd($this->seed, 7) % 3 + 2; $i < $l ; $i++) {
		   $n = (int)($sb * 0.01 * count($this->list));;
		   $a = $this->list[$n];
		   $m = (int)($this->rnd($this->seed,(2 + $i)) % 100 * 0.01 *  count($this->avatars));
		   $mess .=  getface($this->avatars[$m]) ."【". $a->name."】". $a->bad."\n";
			array_splice($this->list,$n, 1);
			array_splice($this->avatars,$m, 1);
	};
		//echo $mess; date*/
		//$this->message = "{date} {week}\n".$this->GetLunarDay()."\n".$mess;
    $this->message = "[img]".$url."[\img]";
    }
    private function getSeed() {
    	//echo $this->year * 37621;
    	$this->seed = $this->year * 37621 + ($this->month) * 539 + $this->day;
    }
    
    private function GetBit($m, $n) {
		return ($m >> $n) & 1;
	}
	
	private function rnd($a, $b){
		 $n = $a % 11117;
		for ( $i = 0; $i < 5 + $b; $i++) {
			$n = $n * $n;
			$n = $n % 11117;
		}
		return $n; 
	}
	
	private function e2c() {
		$isEnd = false;
		$tmp = $this->year-1900;
		if ($tmp < 1900) {
			$tmp += 1900;
		}
		$n = 0;
		$m = 0;
		$k = 0;
		$total = ($tmp - 1921) * 365 + floor(($tmp - 1921) / 4) + $this->madd[$this->month] + $this->day - 38;
		if (($this->year-1900) % 4 == 0 && $this->month > 1) {
			$total++;
		}
		for ($m = 0; $m < 255; $m++) {
			$k = ($this->CalendarData[$m] < 0xfff) ? 11: 12;
			for ($n = $k; $n >= 0; $n--) {
				if ($total <= 29 + $this->GetBit($this->CalendarData[$m], $n)) {
					$isEnd = true;
					break;
				};
				$total = $total - 29 - $this->GetBit($this->CalendarData[$m], $n);
			};
			if ($isEnd){
				break;
			};
		};
		$this->cYear = 1921 + $m;
		$this->cMonth = $k - $n + 1;
		$this->cDay = $total;
		if ($k == 12) {
			if ($this->cMonth == floor($this->CalendarData[$m] / 0x10000) + 1) {
				$this->cMonth = 1 - $this->cMonth;
			}
			if ($this->cMonth > floor($this->CalendarData[$m] / 0x10000) + 1) {
				$this->cMonth--;
			}
		}
	}
	
	private function GetcDateString() {
		$tmp = "";
		$tmp .= $this->charAt($this->tgString ,(($this->cYear - 4) % 10));
		$tmp .= $this->charAt($this->dzString ,(($this->cYear - 4) % 12));
		$tmp .= "";
		$tmp .= $this->charAt($this->sx,(($this->cYear - 4) % 12));
		$tmp .= "年 ";
		if ($this->cMonth < 1) {
			$tmp .= "闰";
			$tmp .= $this->charAt($this->monString,( - $this->cMonth - 1));
		} else {
			$tmp .= $this->charAt($this->monString,($this->cMonth - 1));
		};
		$tmp .= "月";
		$tmp .= ($this->cDay < 11) ? "初": (($this->cDay < 20) ? "十": (($this->cDay < 30) ? "廿": "三十"));
		if ($this->cDay % 10 != 0 || $this->cDay == 10) {
			$tmp .= $this->charAt($this->numString,(($this->cDay - 1) % 10));
		};
		return $tmp;
	}
	
	function GetLunarDay() {
		//solarYear = solarYear<1900?(1900+solarYear):solarYear;
		if ($this->year < 1921 || $this->year > 2020) {
			return "";
		} else {
			$this->month = $this->month > 0 ? ($this->month - 1) : 11;
			$this->e2c();
			return $this->GetcDateString();
		};
	}
	
	private function charAt($str,$x) {
		return substr($str,$x*3,3) ;
	}
	
    private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	} 
}

?>